package com.site.blog.job;

import com.site.blog.config.DbBackUpInfoConf;
import com.site.blog.util.DbBackUtil;
import com.site.blog.util.SpringContextJobUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author : Connor-G 康纳-郭
 * @date : 00:13 20.12.20
 * @Company : http://www.zzrgsoft.org
 * @Version : 1.0
 * <p>
 * 数据库备份
 */
@Slf4j
@Component
public class DbBackUp implements BaseJob {

    public static void main(String[] args) {
      /*  String domain = "www.ykmaiz.com";
        new
        int iVisit = 0;
        String info = String.format("该域名%s被访问了%d次.", domain , iVisit);
        System.out.println(info);*/

       /* LocalDateTime localDateTime = LocalDateTime.now();
        String format = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH:mm:ss"));

        String sql = "mysqldump -u root -proot123 %s >%smy-blog-%s";
        String info = String.format(sql, "my_blog_db", "/opt/boot_jar/my-blog-dbbackup-sql/", format + ".sql");
        System.out.println(info);*/

//        try {
////            DbBackUtil.backup("127.0.0.1", "3306", "root", "123456", "D:/guozx/", "test.sql", "my_blog_db2");
//            DbBackUtil.restore("D:/guozx/test.sql", "127.0.0.1", "3306", "root", "123456", "my_blog_db2");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    @Override
    public void execute(JobExecutionContext context) {
        LocalDateTime localDateTime = LocalDateTime.now();
        String format = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH_mm_ss"));

        // 使用静态工厂获取 bean组件。
        DbBackUpInfoConf dbBackUpInfoConf = (DbBackUpInfoConf) SpringContextJobUtil.getBean("dbBackUpInfoConf");

        // 备份数据库名称
        String dbName = dbBackUpInfoConf.getDbName();
        // 备份路径
        String filePath = dbBackUpInfoConf.getFilePath();
        // 数据库密码
        String password = dbBackUpInfoConf.getPassword();
        // 数据库账号
        String username = dbBackUpInfoConf.getUsername();
        // 备份脚本
        //String script = dbBackUpInfoConf.getScript();
        // ip
        String host = dbBackUpInfoConf.getHost();
        // 备份sql文件名
        String backFileName = "blog-" + format + ".sql";
        // 使用封装的方法
        try {
            DbBackUtil.backup(host, "3306", username, password, filePath, backFileName, dbName);
        } catch (Exception e) {
            /**
             *  logger.info()其中 {}表示占位符
             *  logger.info("from:{},to:{},amount:{}",from,to,amount);
             */
            log.info("备份数据库 dbName:{}失败!,时间 time:{}", dbName, format);
            e.printStackTrace();
        }

        /*
        File uploadDir = new File(filePath);
        if (!uploadDir.exists())
            uploadDir.mkdirs();
        // window
        //String cmd = "cmd /c mysqldump -u" + username + " -p" + password + " " + dbName + " > " + filePath + "/" + dbName + "_my-blog.sql";
        // linux
        //String shell = "mysqldump -u root -proot123 my_blog_db >/opt/boot_jar/my-blog-dbbackup-sql/test3.sql";
        // mysqldump -h127.0.0.1 -P3306 -uroot -proot test>C:\reyo\backupDatabase\test-20190516.sql
        // mysqldump -h127.0.0.1 -P3306 -uroot -proot123 my_blog_db>/opt/boot_jar/my-blog-dbbackup-sqlmy-blog-2021-01-14-16:44:45.sql
        String scriptShell = String.format(script, username, password, dbName, filePath, format + ".sql");
        log.info("script " + scriptShell);
        try {
            Process process = Runtime.getRuntime().exec(scriptShell);
            if (process.waitFor() == 0) {
                log.info("备份数据库数据成功，备份时间为：" + format);
            }
        } catch (Exception e) {
            log.info("备份数据库数据失败，备份时间为：" + format);
            e.printStackTrace();
        }
        */
    }
}
